package cj.com.chabaike.ui.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import cj.com.chabaike.R;
import cj.com.chabaike.bll.RWServer;


public class WelcomePager extends Activity {
	
	
	Handler handle = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Intent intent ;
			
			switch (msg.what) {
			case 0:  //first start
				intent=new Intent("explainpager");
				startActivity(intent);
				overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
				rws=null;
				WelcomePager.this.finish();
				break;
			case 1:
				//intent=new Intent("newsmainpager");
				intent=new Intent("explainpager");
				startActivity(intent);
				overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
				rws=null;
				WelcomePager.this.finish();
				Log.e("welcomepager", "docontinue");
				break;

			default:
				break;
			}
		}
		
	};
	
	RWServer rws;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	setContentView(R.layout.welcome);
		rws=RWServer.getInstance(this.getApplicationContext());
		
		if(RWServer.isFirstUse()){
			doFirstUse();
			Log.i("Wel", "first time");
		}else{
			doContinueUse();
			Log.i("Wel", " continue");
		}
	}

	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		Toast.makeText(this,"载入中",Toast.LENGTH_SHORT).show();
	}

	private void doContinueUse() {
		initDB(1);
		
	}
	private void doFirstUse() {
		initDB(0);
		
	}
	
	private void initDB(final int what) {
		// TODO Auto-generated method stub
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				
				try {
					Thread.sleep(1500);
					handle.sendEmptyMessageAtTime(what, 1500);
					rws.setContiueUse();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		
		
	}
	
}
